﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_LuuLuongDTController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }

        #region Index
        //public ActionResult Index(ViewModelSLLDT_FORM SearchModel, int? currentPage)
        //{
        //    //initialCategorySearchAction(SearchModel);
          
        //    //ViewBag.TotalRow = entities.Count();
        //    //var model = new List<SLLDT>();
        //    //foreach (var currentEntity in entities)
        //    //{
        //    //    model.Add(Mapper.Map<SLLDT>(currentEntity));
        //    //}
        //    //Int16 icount = 1;
        //    //foreach (var item in model)
        //    //{
        //    //    item.STT = icount;
        //    //    item.LLDT_ACTIVE = item.LLDT_ACTIVE == "1" ? "Hiệu lực" : "Không hiệu lực";
        //    //    icount++;
        //    //}
        //    //var pageIndex = SearchModel.Page ?? 1;
        //    //SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
        //    //return View(SearchModel);
        //}

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSLLDT_FORM viewModel)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pLLDT_DVGTVT_MA);
            viewModel.pLLDT_DVGTVT_MA = getdvgtvt_Ma;

            ViewBag.lstHangGP = new SelectList(_db.Filter<SHANGGPLX>(x => x.HGPLX_MA != "A1" && x.HGPLX_MA != "A2" && x.HGPLX_MA != "A3" && x.HGPLX_MA != "A4"), "HGPLX_MA", "HGPLX_TEN");
            //viewModel.listTrangThai = _db.GetAll<STRANGTHAI>();
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSHOSO());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSHOSO model, FormCollection form)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SHOSO _model = new SHOSO();
                var str = Convert.ToInt64(_db.GetAll<SHOSO>().Max(x => x.HOSO_MA).ToString());
                GPS.COMMON.Helpers.CopyObject<SHOSO>(model, ref _model);
                _model.HOSO_MA = GetValue_Indentity();
                _db.Insert<SHOSO>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.HOSO_HTGPLX_MAss = new SelectList(_db.GetAll<SHTCAPGPLX>(), "HTGPLX_MA", "HTGPLX_TEN");
            ViewBag.HOSO_NDSH_MAss = new SelectList(_db.GetAll<SNOIDUNGSH>(), "NDSH_MA", "NDSH_TEN");
            ViewBag.HOSO_NHOMSS = new SelectList(_db.Filter<SHOSO>(x => x.HOSO_NHOM == "0"), "HOSO_MA", "HOSO_TEN");
        }

        public string GetValue_Indentity()
        {
            string value = "";
            Int64 valueMax = _db.DbContext.SHOSOes.ToList().Max(e => Convert.ToInt64(e.HOSO_MA));
            value = (valueMax + 1).ToString();
            return value;
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id)
        {
            CrudModelSHOSO _model = new CrudModelSHOSO();
            SHOSO model = _db.GetOne<SHOSO>(o => o.HOSO_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHOSO>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSHOSO model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SHOSO>(o => o.HOSO_MA == model.HOSO_MA);
                GPS.COMMON.Helpers.CopyObject<SHOSO>(model, ref _model);
                _db.Update<SHOSO>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSHOSO view)
        {
            ViewBag.HOSO_HTGPLX_MAss = new SelectList(_db.GetAll<SHTCAPGPLX>(), "HTGPLX_MA", "HTGPLX_TEN");
            ViewBag.HOSO_NDSH_MAss = new SelectList(_db.GetAll<SNOIDUNGSH>(), "NDSH_MA", "NDSH_TEN");
            string selectedValuesNhom = _db.GetAll<SHOSO>().Select(x => x.HOSO_NHOM = view.HOSO_NHOM).FirstOrDefault();
            ViewBag.HOSO_NHOMSS = new SelectList(_db.Filter<SHOSO>(x => x.HOSO_NHOM == "0"), "HOSO_MA", "HOSO_TEN", selectedValuesNhom);
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id)
        {
            CrudModelSHOSO _model = new CrudModelSHOSO();
            SHOSO model = _db.GetOne<SHOSO>(o => o.HOSO_MA == id);
            _db.DeleteItem<SHOSO>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id)
        {
            CrudModelSHOSO _model = new CrudModelSHOSO();
            SHOSO model = _db.GetOne<SHOSO>(o => o.HOSO_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHOSO>(model, ref _model);
            SHTCAPGPLX htcap = _db.GetOne<SHTCAPGPLX>(o => o.HTGPLX_MA == _model.HOSO_HTGPLX_MA);
            SNOIDUNGSH nd = _db.GetOne<SNOIDUNGSH>(o => o.NDSH_MA == _model.HOSO_NDSH_MA);

            if (htcap != null)
            {
                _model.HOSO_HTGPLX_MA = htcap.HTGPLX_TEN == null ? null : htcap.HTGPLX_TEN;
            }
            if (nd != null)
            {
                _model.HOSO_NDSH_MA = nd.NDSH_TEN == null ? null : nd.NDSH_TEN;
            }

            if (_model.HOSO_ACTIVE == "1")
            {
                _model.HOSO_ACTIVE = "Hiệu Lực";
            }
            else
            {
                _model.HOSO_ACTIVE = "Không Hiệu Lực";
            }
            return PartialView("Detail", _model);
        }
        #endregion

        public List<CrudModelSLLDT_FROM> GetListData(ViewModelSLLDT_FORM SearchModel)
        {
            var _pID = new OracleParameter("pID", OracleDbType.Decimal, (SearchModel.pID == null ? DBNull.Value : (object)SearchModel.pID), ParameterDirection.Input);
            var _pLLDT_DVGTVT_MA = new OracleParameter("pLLDT_DVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pLLDT_DVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pLLDT_DVGTVT_MA), ParameterDirection.Input);
            var _pLLDT_HGPLX_MA = new OracleParameter("pLLDT_HGPLX_MA", OracleDbType.Varchar2, (SearchModel.pLLDT_HGPLX_MA == null ? DBNull.Value : (object)SearchModel.pLLDT_HGPLX_MA), ParameterDirection.Input);
            var _pLLDT_ACTIVE = new OracleParameter("pLLDT_HGPLX_MA", OracleDbType.Varchar2, (SearchModel.pLLDT_ACTIVE == null ? DBNull.Value : (object)SearchModel.pLLDT_ACTIVE), ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<CrudModelSLLDT_FROM>("BEGIN GPS_DAOTAO.GPS_DAOTAO_GET_LUULUONGDAOTAO(:pID,:pDVGTVT_MA,:pLLDT_HGPLX_MA, :pLLDT_ACTIVE,:pRET); end;"
                , _pID
                , _pLLDT_DVGTVT_MA
                                                                                                                            , _pLLDT_HGPLX_MA
                                                                                                                            , _pLLDT_ACTIVE
                                                                                                                            , _pRET).ToList();
            return entities;
        }
    }
}